package com.power.rpc;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.power.leopard.remote.ProviderConfig;
import com.power.leopard.remote.ServerConfig;
import com.power.leopard.remote.ServiceFactory;

public class ServiceRegistry {

	public ServiceRegistry() {
	}

	public ServiceRegistry(int port) {
		this.port = port;
	}

	private Map<String, Object> services;
	private int port = 20000;

	public void setServices(Map<String, Object> services) {
		this.services = services;
	}

	public void init() {
		ServerConfig serverConfig = new ServerConfig();
		serverConfig.setPort(port);
		List<ProviderConfig<?>> providers = new ArrayList<ProviderConfig<?>>();
		for (String url : services.keySet()) {
			ProviderConfig<Object> providerConfig = new ProviderConfig<Object>(services.get(url));
			providerConfig.setUrl(url);
			providerConfig.setServerConfig(serverConfig);
			providers.add(providerConfig);
		}
		ServiceFactory.addServices(providers);
		System.out.println(services);
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

}
